package com.tj.jinzao.fragment;

import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tj.jinzao.R;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.activity.LoginActivity;
import com.tj.jinzao.activity.MainActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.RegisterLoginPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.utils.Utils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.tj.jinzao.activity.LoginActivity.LOGIN_TYPE_INPUT_VERIFY_CODE_RESET;
import static com.tj.jinzao.activity.LoginActivity.LOGIN_TYPE_VERITY_LOGIN;

/**
 * 密码登录
 * Created by wjy on 2019/1/5.
 */
public class PasswordLoginFragment extends BaseFragment<RegisterLoginPresenter> implements IRegisterLoginView<User> {
    @BindView(R.id.et_phone)
    EditText mPhoneEditText;
    @BindView(R.id.et_password)
    EditText mPasswordView;
    @BindView(R.id.tv_protool)
    TextView mProtoolView;
    @BindView(R.id.tv_forget)
    TextView mForgetView;
    private IWXAPI api;
    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
//        if (BuildConfig.DEBUG) {
//            mPhoneEditText.setText("13818485973");
//        }
        mProtoolView.setText(Html.fromHtml(getString(R.string.login_protool)));
        mForgetView.setText(Html.fromHtml(getString(R.string.login_forget_code)));
        api = WXAPIFactory.createWXAPI(getActivity(), Constant.WX_APPID);
        api.registerApp(Constant.WX_APPID);

    }

    @Override
    protected RegisterLoginPresenter createPresenter() {
        return new RegisterLoginPresenter(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_password_login;
    }

    @OnClick(R.id.tv_verity_login)
    public void verityLogin() {
        if (mContext instanceof LoginActivity) {
            ((LoginActivity) mContext).switchFragment(LOGIN_TYPE_VERITY_LOGIN);
        }
    }

    @OnClick(R.id.tv_forget)
    public void forget() {
        String phone = mPhoneEditText.getText().toString().trim();
        if (Utils.isPhone(phone)) {
            mPresenter.sendCode(phone, "reset");
        } else {
            ToastUtils.showToast(mContext, "请输入正确手机号");
        }

    }
    @OnClick({R.id.iv_weixin, R.id.iv_qq, R.id.iv_weibo})
    public void authorize(View view) {
        switch (view.getId()) {
            case R.id.iv_weixin:
                SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wx_login_state";
                api.sendReq(req);

//                mPresenter.getUserByOpenId(platform.getDb().getUserId());
                break;
            case R.id.iv_qq:

                break;
            case R.id.iv_weibo:

                break;
        }
    }

    @OnClick(R.id.bt_login)
    public void login() {
        String phone = mPhoneEditText.getText().toString().trim();
        String password = mPasswordView.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showToast(mContext, "请输入密码");
            return;
        }
        if(mPasswordView.getText().toString().length()<6||mPasswordView.getText().toString().length()>18){
            ToastUtils.showToast(mContext, "密码长度在6-18位之间");
            return;
        }
        if (Utils.isPhone(phone)) {
            mPresenter.loginByPassword(phone, password);
        } else {
            ToastUtils.showToast(mContext, "请输入正确手机号");
        }
    }

    @Override
    public void onRequestSuccess(User user) {
        if (TextUtils.isEmpty(user.getNickname())) {
            startActivity(new Intent(mContext, CompleteInfoActivity.class));
        } else {
            startActivity(new Intent(mContext, MainActivity.class));
        }
        mContext.finish();
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {
        ToastUtils.showToast(mContext, error_message);
    }

    @Override
    public void onGetCodeSuccess(String s) {
        if (mContext instanceof LoginActivity) {
            ((LoginActivity) mContext).setPhone(mPhoneEditText.getText().toString().trim());
            ((LoginActivity) mContext).switchFragment(LOGIN_TYPE_INPUT_VERIFY_CODE_RESET);
        }
    }

    @Override
    public void onGetCodeFailed(String reason) {
        //发送验证码失败，此处不用
        if(reason==null){
            ToastUtils.showToast(getActivity(),"发送验证码失败");
        }else {
            ToastUtils.showToast(getActivity(),reason);
        }
    }

    @Override
    public void onCheckCodeSuccess(String token) {

    }
}
